package sy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sy.pageModel.BaseEntity;
import sy.pageModel.DataGrid;
import sy.pageModel.DeptInfo;
import sy.pageModel.Json;
import sy.service.InfoServiceI;
import sy.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016-06-29.
 */
@Controller
@RequestMapping("infoController")
public class InfoController extends BaseController {
    @Autowired
    private InfoServiceI infoService;

    @Autowired
    private HttpSession session;
    @RequestMapping("/datagrid.action")
    @ResponseBody
    public DataGrid findPartnerAction(DeptInfo info){
        return infoService.detagrid(info);
    }

    /**
     * 添加单位所属栏目
     * @param info
     * @return
     */
    @RequestMapping("/add_info.action")
    @ResponseBody
    public Json addInfoAction(DeptInfo info){
        int rows=0;
        Map map=StringUtil.findMap(info.getPic());
        info.setPic((String) map.get("pic"));
        info.setId((String) map.get("id"));
        try {
            rows=infoService.addInfoService(info);
            if (rows>0) logUserAction(session,"infoController/add_info.action",info.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"添加");
        return js;
    }

    /**
     * 修改单位所属栏目
     * @param info
     * @return
     */
    @RequestMapping("/edit_info.action")
    @ResponseBody
    public Json editInfoAction(DeptInfo info){
        int rows=0;
        info.setPic(StringUtil.findStr(info.getPic()));
        try {
            rows=infoService.editInfoService(info);
            if (rows>0) logUserAction(session,"infoController/edit_info.action",info.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"修改");
        return js;
    }

    /**
     * 删除单位所属栏目
     * @param info
     * @return
     */
    @RequestMapping("/del_info.action")
    @ResponseBody
    public Json delInfoAction(DeptInfo info){
        int rows=0;
        try {
             rows= infoService.deleteInfoService(info);
            if (rows>0)   logUserAction(session,"infoController/edit_info.action",info.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"删除");
        return js;
    }



    @RequestMapping("/find_info.action")
    @ResponseBody
    public List<BaseEntity> findDeptType(String ids){
        return infoService.findList(ids);
    }
}
